# -*- coding: UTF-8 -*-
# @Date ：2024/6/27 18:02
# @File：main.py
# @Author：mmmme
# @IDE：PyCharm

import sys
import os
import threading
import time

from src.config.log_config import initLogConf, getLogger
from src.service.jy_http_push_service import JyHttpPushService
from src.service.tjkj_get_http_data import TjkjGetHttpData

logger = getLogger(__name__)


def get_executable_directory():
    """
    获取当前工作目录
    :return:
    """
    if getattr(sys, 'frozen', False):
        return os.path.dirname(sys.executable)
    else:
        return os.path.dirname(os.path.abspath(__file__))


company_config = [
    # {
    #     'company_name': '山东亿泰化工',
    #     'company_code': '371310318',
    #     'appId': 'yitaihuagong',
    #     'appSecret': 'c9f7876db9b84104af2adec14c9c2161',
    #     'dual_url':"http://temp.saas.tjkj.pro:82",
    #     'job_url': "http://saas.newspo.tjkj.pro:82",
    #     'location_url': "http://saas.newspo.tjkj.pro:82",
    #     'job_token': 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjhlNDg2OWQ0LTEyMjYtNDFhOC04MmJiLTQ1NjM5MzYwZjgxZSJ9.bAsqbySsdZ7e-W0jkWpU51ae9H76CViS2LWtMk3053Amvs-VFaukHMLtoGl02RGAnTP1PuRnPQavZJnQV7chBA',
    #     'token': 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjcxYmM1NzY3LTJhZjItNDA5Ni04ZWI0LWIwZjgxNTA3N2IzYyJ9.LMlaCAmVz5vK0xFuXT2CkK73ieyJ9SJmmSbNKrNWBIFw-aTevKMW1l7rU-8bXmZMXABVZhR3KvOrw9sy8uXEQw',
    #     'location_token': 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjFhY2Q0NDI1LTE1NzEtNDViNy1iN2EwLTU5MGY3ZDFkZTVhZSJ9.brojBnGX8mRz2cmtHSe3r1WSuZ76lIh2YnOgW_8yLKBjMslLS-8a7-_UMANQJ6_Y52lWCgF3wFmEus2HP9_clw'
    # }
    # ,
    {
        'company_name': '山东博沂化工',
        'company_code': '371310044',
        'appId': 'boyihuagong',
        'appSecret': 'baf6769b72454302b5573dfe40bc5013',
        'dual_url':"http://temp.saas.tjkj.pro:82",
        'job_url': "http://saas.newspo.tjkj.pro:82",
        'location_url': "http://saas.tjkj.pro:82",
        'job_token': 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjEwOGQ2NWEwLTY4YjMtNGNkMi05ZTRmLTVhMDQzNzA2M2ExYyJ9.nevZG1mPWbi1P74JxRW5sWY0YDI82gMTajDFlQbN0VHIqJjP8RZ8tar3KshAjrJfdBR9TioTFA1fPlItm8UBdw',
        'location_token': 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImViNThiNmIzLTdmMDItNDI1Mi1iZjFlLWM3NGRkNGZmNTY2ZiJ9.te5si9DZEU8EawnB-llLKFaJdDNRXgy46yJW4XHCrQoO2zuWb21cVGVDcsB-t5LY8PfuwjwKVx_ygUwL2q_QQA',
        'token': 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjM0NzEwNTdkLTljM2UtNDdhMS05YjM1LWQ2ZGU2MTkyNjRkZSJ9.6-PipEnIwFhnvN31n9hn2dFLdRZQp75DN4gotTOPuzYJYxiTiHk4NzqaJphxO58KHaiHxlUxBMTuaIISDncugw'
    }
    # ,
    # {
    #     'company_name': '临沂润泽环保',
    #     'company_code': '371310250',
    #     'appId': 'runzehuanbao',
    #     'appSecret': '58f0b4a58f2346d0b9e1f5a5d6ae48db',
    #     'dual_url': "http://saas.tjkj.pro:82",
    #     'job_url': "http://saas.newspo.tjkj.pro:82",
    #     'location_url': "http://saas.tjkj.pro:82",
    #     'job_token': 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImRhYjY1Mjk3LTk4ZjQtNGJlZC1iZTNhLTRjMjk1ODcyZjU4NSJ9.G4riA7b7jLrFsC2Ca9JuJ5cN1HexP1LrAlmPUgt-qUt-bhLaVka0zoRVfBUXoGgkGXnxpfETD_yIOgs7XQTEvg',
    #     'location_token': 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6Ijk5MzY3ZjNmLTBlNjItNGVkMC1hZDhkLTZjZmNmMmE4NTIzNCJ9.S4aT_dKUisI4p0TvV_-Y6AsXb_uq4syV77kQ5y59k-ORvFIamAKjivhxAO3cZjk05DINYZcYyhGXuBzMLRoIAw',
    #     'token': 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImIxNTgzNDQyLWI3YTctNDc0OC1iY2VjLTVlZDczNmNiZTE4YiJ9.vpKcX-eThv2MA-8mCA4DImfCHL1Qv7ohwqplYAl_yl_agq_bbGQrbwEBI4iAJJq13b32uORr4FH0K7CoJRYvGw'
    # }
]


def ready_push_to_jy(cof):
    tjkj = TjkjGetHttpData(dual_token=cof.get("token"), job_token=cof.get("job_token"),
                           dual_url=cof.get("dual_url"), job_url=cof.get("job_url"),
                           location_url=cof.get("location_url"),
                           location_token=cof.get("location_token"))
    jy = JyHttpPushService(appId=cof.get('appId'), appSecret=cof.get("appSecret"),
                           company_name=cof.get("company_name"), company_code=cof.get("company_code"), TJKJ=tjkj)
    jy.get_risk_analysis_unit()
    jy.get_risk_event()
    jy.get_risk_control_measures()
    jy.get_hazard_detection_task()
    jy.get_hazard_detection_record()
    jy.get_hazard_detection_information()
    jy.get_job_ticket_information()
    jy.get_person_real_time_location()


def start_thread():
    try:
        for cof in company_config:
            threading.Thread(target=ready_push_to_jy, args=(cof,), daemon=True, name=cof['company_name']).start()
        while True:
            time.sleep(10)
    except KeyboardInterrupt:
        logger.error(f"app closed!")


if __name__ == '__main__':
    initLogConf(get_executable_directory(), "DEBUG")
    start_thread()
